package com.chalmers.foa.gamemodel.gameobjects.other.impl;

import android.graphics.Bitmap;
import android.graphics.Point;

import com.chalmers.foa.gamehandlers.impl.PhysicsHandler;
import com.chalmers.foa.gamemodel.gameobjects.BitmapType;
import com.chalmers.foa.gamemodel.gameobjects.DirectionType;
import com.chalmers.foa.gamemodel.gameobjects.GameObjectBitmapAdapter;
import com.chalmers.foa.util.BitmapInitiator;

public class PlayerObject extends GameObjectBitmapAdapter {
	
	private DirectionType gravityDirection;
	private DirectionType movingDirection;
	private boolean directionChanged;
	private Bitmap[] bitmaps;
	
	public PlayerObject(Bitmap bitmap) {
		this(new Point(0,0),bitmap,0,-1);
	}

	public PlayerObject(Point _position,Bitmap _bitmap,int layer,int bound){
		super(_bitmap,_position,layer,bound, false,true, false, false);
		gravityDirection = DirectionType.DOWN;
		movingDirection = DirectionType.RIGHT;
		directionChanged = false;
		bitmaps = new Bitmap[4];
		BitmapInitiator.initiateBitmaps(bitmaps, _bitmap);
	}
	@Override
	public Bitmap getBitmap(){
		return bitmap;
	}
	
	public void setDirectionChanged(boolean directionChanged) {
		this.directionChanged = directionChanged;
	}
	
	public void setGravityDirection(DirectionType direction) {
		gravityDirection = direction;
	}
	
	public void setMovingDirection(DirectionType direction) {
		movingDirection = direction;
	}
	
	public DirectionType getGravityDirection() {
		return gravityDirection;
	}
	
	public DirectionType getMovingDirection() {
		return movingDirection;
	}
	
	private void setBitmap(BitmapType state){
		bitmap = bitmaps[state.ordinal()];
	}
	
	
	
	@Override
	public void update() {
		if(directionChanged) {
			if(movingDirection == DirectionType.RIGHT && 
					PhysicsHandler.getInstance().getGravity() > 0)
		    	setBitmap(BitmapType.DownRight);
		    else if(movingDirection == DirectionType.LEFT &&
		    		PhysicsHandler.getInstance().getGravity() > 0)
	    	setBitmap(BitmapType.DownLeft);
		    else if(movingDirection == DirectionType.RIGHT && 
		    		PhysicsHandler.getInstance().getGravity() < 0)
		    	setBitmap(BitmapType.UpRight);
		    else if(movingDirection == DirectionType.LEFT && 
		    		PhysicsHandler.getInstance().getGravity() < 0)
		    	setBitmap(BitmapType.UpLeft);
		}
	}
	
	@Override
	public void revertToStartPosition() {
		super.revertToStartPosition();
		setAlive(true);
	}
	
}
